﻿CREATE PROCEDURE [dbo].[usp_insert_Fault]
(
			 @FaultID				   uniqueidentifier
			,@NativeMessageID		   varchar(256)
			,@ActivityID			   varchar(256)
			,@Application			   varchar(256)
			,@Description			   varchar(MAX)
			,@ErrorType				   varchar(256)
			,@FailureCategory		   varchar(256)
			,@FaultCode				   varchar(256)
			,@FaultDescription		varchar(MAX)	= NULL
			,@FaultSeverity			int				= NULL
			,@Scope					   varchar(256)
			,@ServiceInstanceID		varchar(256)
			,@ServiceName			   varchar(256)
			,@FaultGenerator		   varchar(256)		= NULL
			,@MachineName			   varchar(256)		= NULL
			,@DateTime				   datetime		   = NULL
			,@ExceptionMessage		varchar(MAX)
			,@ExceptionType			varchar(256)
			,@ExceptionSource		   varchar(256)
			,@ExceptionTargetSite	varchar(256)
			,@ExceptionStackTrace	varchar(MAX)
			,@InnerExceptionMessage	varchar(MAX)
			,@ItineraryUuid			varchar(40)		= NULL
			,@ServiceUuid			varchar(40)		= NULL
			,@ServicePosition			int		= NULL
			,@ItineraryName			nvarchar(256)		= NULL
			,@ItineraryVersion			nvarchar(32)		= NULL
			,@Debug					   bit			   = 0
)
AS

/****************************************************************************
Name: usp_insert_Fault.sql
Description: Inserts an ESB Fault Message into the Fault table
	table.
*****************************************************************************/
SET NOCOUNT ON;

-- Bootstrap
DECLARE @DebugPrefix       varchar(10);
DECLARE @ErrorCode         int;

SET @ErrorCode          = 0;
SET @DebugPrefix        = '>>> DEBUG: ';


-- Test harness
/*
DECLARE @FaultID				      uniqueidentifier
DECLARE @NativeMessageID			  varchar(128)
DECLARE @Application			      varchar(50)
DECLARE @Description			      varchar(100)
DECLARE @ErrorType				   varchar(50)
DECLARE @FailureCategory		   varchar(50)
DECLARE @FaultCode				   varchar(50)
DECLARE @FaultDescription		   varchar(100)	= NULL
DECLARE @FaultSeverity			   int				= NULL
DECLARE @Scope					      varchar(50)
DECLARE @ServiceInstanceID		   varchar(50)
DECLARE @ServiceName			      varchar(50)
DECLARE @FaultGenerator			   varchar(50)		= NULL
DECLARE @MachineName			      varchar(50)		= NULL
DECLARE @DateTime				      datetime		   = NULL
DECLARE @ControlBit				   varchar(50)		= NULL
DECLARE @ExceptionMessage		   varchar(100)
DECLARE @ExceptionType			   varchar(50)
DECLARE @ExceptionSource		   varchar(50)
DECLARE @ExceptionTargetSite	   varchar(50)
DECLARE @ExceptionStackTrace	   varchar(50)
DECLARE @InnerExceptionMessage	varchar(100)
DECLARE @InsertedDate			   datetime
DECLARE @InsertedBy				   varchar(50)
DECLARE @Debug					      bit				= 1

SELECT  @FaultID =
,@Application			   
,@Description			   
,@ErrorType				   
,@FailureCategory		   
,@FaultCode				   
,@FaultDescription		
,@FaultSeverity			
,@Scope					   
,@ServiceInstanceID		
,@ServiceName			   
,@FaultGenerator		   
,@MachineName			   
,@DateTime				   
,@ControlBit			   
,@ExceptionMessage		
,@ExceptionType			
,@ExceptionSource		   
,@ExceptionTargetSite	
,@ExceptionStackTrace	
,@InnerExceptionMessage	
,@InsertedDate			   
,@InsertedBy			   
,@Debug					   

*/

-- Print parameters
/*IF (@Debug > 0)
BEGIN
	PRINT @DebugPrefix + '@FaultID = '           + CONVERT(varchar, @FaultID);
	PRINT @DebugPrefix + '@Application = '       + @Application;
	PRINT @DebugPrefix + '@ErrorType = '         + @ErrorType;
	PRINT @DebugPrefix + '@FailureCategory = '   + @FailureCategory;
   PRINT @DebugPrefix + '@FaultCode = '         + @FaultCode;
END;*/


INSERT INTO dbo.Fault
	   (
		FaultID
	   ,NativeMessageID
	   ,ActivityID
	   ,[Application]
	   ,[Description]
	   ,ErrorType
	   ,FailureCategory
	   ,FaultCode
	   ,FaultDescription
	   ,FaultSeverity
	   ,Scope
	   ,ServiceInstanceID
	   ,ServiceName
	   ,FaultGenerator
	   ,MachineName
	   ,[DateTime]
	   ,ExceptionMessage
	   ,ExceptionType
	   ,ExceptionSource
	   ,ExceptionTargetSite
	   ,ExceptionStackTrace
	   ,InnerExceptionMessage
	   ,InsertMessagesFlag
	   ,ItineraryUuid
	   ,ServiceUuid
	   ,ServicePosition
	   ,ItineraryName
	   ,ItineraryVersion
	   )
 VALUES
	   (
		 @FaultID
		,@NativeMessageID
		,@ActivityID
		,@Application			   
		,@Description			   
		,@ErrorType				   
		,@FailureCategory		   
		,@FaultCode				   
		,@FaultDescription		
		,@FaultSeverity			
		,@Scope					   
		,@ServiceInstanceID		
		,@ServiceName			   
		,@FaultGenerator		   
		,@MachineName			   
		,@DateTime			   
		,@ExceptionMessage		
		,@ExceptionType			
		,@ExceptionSource		   
		,@ExceptionTargetSite	
		,@ExceptionStackTrace	
		,@InnerExceptionMessage	
		,1
		,@ItineraryUuid
		,@ServiceUuid
		,@ServicePosition
		,@ItineraryName
		,@ItineraryVersion
		)

-- CHECK FOR ERROR
SET @ErrorCode = @@Error;

IF (@ErrorCode <> 0) GOTO ERROR_HANDLER;

RETURN 0;

-- Handles errors.
ERROR_HANDLER:
IF (@ErrorCode <> 0)
BEGIN
    IF (@Debug > 0) 
		BEGIN
			PRINT @DebugPrefix + 'Error Code = ' + CAST(@ErrorCode AS varchar(10)) + ' occurred.';
			RETURN @ErrorCode
		END

    -- Don't return any evidence of what specific error occurred, i.e. 
    -- we want to ward off potential hackers.
    RETURN 1;
END;

SET NOCOUNT OFF;
